﻿Imports System.Data.OleDb
Public Class Events
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
            'Counting activity
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            Dim SqlString As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Events")
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            oleDbConn.Close()
        End If

    End Sub

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        If (e.AffectedRows < 13) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("EventName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("BeachName", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM EventAttendance WHERE " & buttonStr & " LIKE '%" & searchStr & "%' ORDER BY ID DESC"
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Sub Button_Click(sender As Object, e As EventArgs)
        'Using a single method for all buttons from listview.
        Dim btn As Button = DirectCast(sender, Button)

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlCheckString As String = "SELECT * FROM Event_Attendees WHERE Attendee = @f1 AND Event_FK = @f2"
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlCheckString, oleDbConn)
        cmdCheck.CommandType = CommandType.Text
        cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdCheck.Parameters.AddWithValue("@f2", btn.CommandArgument)
        oleDbConn.Open()
        'Getting records (only 1 line for selected tourist)
        Dim dr As OleDbDataReader = cmdCheck.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            'Member is already signed in for this event
            If Not IsDBNull(dr("Event_FK")) Then
                btn.Enabled = False
                btn.Text = "Already In!"
                oleDbConn.Close()
                oleDbConn.Dispose()
            End If
        Else
            'If member is not signed in and event is full
            If eventIsFull(btn.CommandArgument) = True Then
                btn.Text = "Full"
                btn.Enabled = False
                oleDbConn.Close()
                oleDbConn.Dispose()
            Else
                'If member is not signed in and event is not full
                Dim SqlString As String = "INSERT INTO [Event_Attendees] ([Event_FK], [Attendee]) VALUES (@f1, @f2)"
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", btn.CommandArgument)
                cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
                cmd.ExecuteNonQuery()
                oleDbConn.Close()
                oleDbConn.Dispose()
                SqlDataSource2.SelectCommand = "SELECT * FROM [EventAttendance] ORDER BY ID DESC"
                SqlDataSource2.DataBind()
                ListView2.DataBind()
            End If
        End If
    End Sub

    Protected Function eventIsFull(ByVal eventID As Integer) As Boolean
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlCheckString As String = "SELECT * FROM EventAttendance WHERE ID = @f1"
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlCheckString, oleDbConn)
        cmdCheck.CommandType = CommandType.Text
        cmdCheck.Parameters.AddWithValue("@f1", eventID)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmdCheck.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("TotalAttendees")) Then
                If dr("TotalAttendees") = dr("Capacity") Then
                    Return True
                Else
                    Return False
                End If
            End If
        End If
        oleDbConn.Close()
        Return True
    End Function

    Protected Function getAttendance(ByVal attendees As Integer, ByVal capacity As Integer) As String
        If attendees = capacity Then
            Return "Event Full"
        Else
            Return "Attendance: " & attendees & "/" & capacity
        End If
    End Function

    Protected Function getEventTime(ByVal eventTime As DateTime) As String
        Dim stringDate As String
        stringDate = eventTime.ToString("dd-MM-yyyy")
        Return stringDate

    End Function

    Protected Sub ListView2_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub
End Class